package com.cskaoyan.servlet;

import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.service.OrderService;
import com.cskaoyan.service.OrderServiceImpl;
import com.cskaoyan.util.ArrayUtil;
import com.cskaoyan.util.JacksonUtil;
import com.cskaoyan.util.StringUtil;
import com.cskaoyan.util.VendorsProperties;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@WebServlet("/admin/order/*")
public class AdminOrderServlet extends WdBaseServlet {
    private OrderService adminOrderService = new OrderServiceImpl();

    @SneakyThrows
    public BaseRespVo list(HttpServletRequest request, HttpServletResponse response) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("开始");
        String pageStr = request.getParameter("page");
        int page = Integer.parseInt(pageStr);
        String limitStr = request.getParameter("limit");
        int limit = Integer.parseInt(limitStr);
        String[] timeStrArrays = request.getParameterValues("timeArray");
        String[] orderStatusStrArrays = request.getParameterValues("orderStatusArray");
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");
        String startStr = request.getParameter("start");
        Date start = StringUtil.isEmpty(startStr) ? null : simpleDateFormat.parse(request.getParameter("start"));
        // Date start = null;
        String endStr = request.getParameter("end");
        Date end = StringUtil.isEmpty(endStr) ? null : simpleDateFormat.parse(request.getParameter("end"));
        String userIdStr = request.getParameter("userId");
        String remoteAddr = request.getRemoteAddr();


        String orderSnStr = request.getParameter("orderSn");

        System.out.println("调用list方法");
        SimpleDateFormat df = new SimpleDateFormat();

        List<Short> orderStatusArray = new ArrayList<>();
        String[] orderStatusArrays = request.getParameterValues("orderStatusArray");
        if (!ArrayUtil.isEmpty(orderStatusArrays)) {
            orderStatusArray = Arrays.asList(orderStatusArrays).stream().map(Short::valueOf).collect(Collectors.toList());
        }
        BaseRespVo baseRespVo = adminOrderService.list(remoteAddr,page,limit,sort,order,userIdStr,orderSnStr,start,end,orderStatusArray);

        return baseRespVo;
    }

    public BaseRespVo detail(HttpServletRequest request, HttpServletResponse response) {
        String idStr = request.getParameter("id");
        Integer id=Integer.parseInt(idStr);

        BaseRespVo baseRespVo =adminOrderService.detail(id);
        return baseRespVo;
    }
    @SneakyThrows
    public BaseRespVo refund(HttpServletRequest request, HttpServletResponse response) {
        String s = request.getReader().readLine();
        Map map = JacksonUtil.read(s, Map.class);
        String remoteAddr = request.getRemoteAddr();


        BaseRespVo baseRespVo =adminOrderService.refund(map,remoteAddr);
        return baseRespVo;
    }
    public BaseRespVo channel(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, String>> vendors = VendorsProperties.getVendors();

        return BaseRespVo.ok(vendors);
    }
    @SneakyThrows
    public BaseRespVo ship(HttpServletRequest request, HttpServletResponse response) {
        String s = request.getReader().readLine();
        Map map = JacksonUtil.read(s, Map.class);
        int orderId=(int)map.get("orderId");
        String shipChannel =(String) map.get("shipChannel");
        String shipSn = (String) map.get("shipSn");

        BaseRespVo baseRespVo =adminOrderService.ship(orderId,shipChannel,shipSn);

        return baseRespVo;
    }
    @SneakyThrows
    public BaseRespVo delete(HttpServletRequest request, HttpServletResponse response) {
        String s = request.getReader().readLine();
        Map map = JacksonUtil.read(s, Map.class);
        Integer orderId =(Integer) map.get("orderId");

        BaseRespVo baseRespVo =adminOrderService.delete(orderId);

        return baseRespVo;
    }

}
